package alpha.com.google.code.mechanic.janitor.ui;

import java.util.Set;

import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.set.IObservableSet;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ObservableSetContentProvider;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.google.eclipse.mechanic.MechanicService;
import com.google.eclipse.mechanic.Task;

/**
 *
 * The class is to insert the new preferences to an existing Task of the
 * current activated Context.
 *
 * Last_edited: Oct 8, 2014
 * 
 * @author jingjiejiang
 * @since Oct 5, 2014
 */
public class ExistingTasksWizardPage extends WizardPage {
  
  private Table tblTasks;

  private Set<Task> setTask;
  private CheckboxTableViewer chkTblTasks;
  
  /**
   * Create the wizard.
   */
  public ExistingTasksWizardPage() {
    super("wizardPage");
    setTitle("Preference Recorder Output");
    setDescription("Save new preferences.");
    
    initTaskSet();
  }
  
  private void initTaskSet() {
    
    MechanicService service = (MechanicService) MechanicService.getInstance();
    setTask = service.getAllKnownTasks();
  }
  

  /**
   * The method is to return the Task that the new preferences will be 
   * inserted into.
   *
   * @return
   */
  public Object[] getSelTask() {
    
    return chkTblTasks.getCheckedElements();
  }
  
  @Override
  public boolean canFlipToNextPage() {
    return false;
  }

  /**
   * Create contents of the wizard.
   * @param parent
   */
  public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);

    setControl(container);
    
    Label lblNewLabel = new Label(container, SWT.NONE);
    lblNewLabel.setBounds(10, 0, 59, 14);
    lblNewLabel.setText("Task List:");
    
    chkTblTasks = CheckboxTableViewer.newCheckList(container, SWT.BORDER | SWT.FULL_SELECTION);
    chkTblTasks.addCheckStateListener(new ICheckStateListener() {
      public void checkStateChanged(CheckStateChangedEvent event) {
        
        if (event.getChecked()) {
          chkTblTasks.setAllChecked(false);
          chkTblTasks.setChecked(event.getElement(), true);
        }
        
        updateWizardButtons();
      }
    });
    tblTasks = chkTblTasks.getTable();
    tblTasks.setHeaderVisible(true);
    tblTasks.setBounds(10, 20, 570, 252);
    
    TableViewerColumn tblViewerClmnTaskName = new TableViewerColumn(chkTblTasks, SWT.NONE);
    TableColumn tblClmnTaskName = tblViewerClmnTaskName.getColumn();
    tblClmnTaskName.setWidth(550);
    tblClmnTaskName.setText("      Task Title");
    
    initDataBindings();
    
  }
  
  protected void initDataBindings() {
    
    ObservableSetContentProvider setContentProvider = new ObservableSetContentProvider();
    IObservableMap observeMap = PojoObservables.observeMap(setContentProvider.getKnownElements(), Task.class, "title");
    chkTblTasks.setLabelProvider(new ObservableMapLabelProvider(observeMap));
    chkTblTasks.setContentProvider(setContentProvider);
    IObservableSet selfSet = Properties.selfSet(Task.class).observe(setTask);
    chkTblTasks.setInput(selfSet);
  }
  
  private void updateWizardButtons() {
    
    getWizard().getContainer().updateButtons();
  }
  
  public boolean hasCheckedTask() {
    
    if (chkTblTasks.getCheckedElements().length != 0) {
      
      return true;
    }
    else {
      return false;
    }
  }

}
